package com.cndw.kungfu.model.pvp.war;

import java.util.Collection;

import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.CrossProxySerialize;
import com.cndw.kungfu.model.WarCrossAreaSerialize;
import com.cndw.kungfu.model.WarWorldSerialize;
import com.cndw.kungfu.model.activity.domain.CrossServer;
import com.cndw.kungfu.model.pvp.api.ApiConfig;
import com.cndw.kungfu.model.pvp.api.WarApiClient;

public class WarCoreCenter {


	public static WarCrossData getCrossData(int area) {
		return WarCrossAreaSerialize.getInstance(area).getWarCrossData();
	}

	public static WarCrossData getDataBySid(int serverId) {
		int area = CrossServer.getArea(serverId);
		return getCrossData(area);
	}

	public static void flower(long uid, String name, int type, int sid) {
		WarFlower warFlower = new WarFlower();
		warFlower.setC(type);
		warFlower.setPid(uid);
		warFlower.setPn(name);
		warFlower.setSid(sid);
		warFlower.setT(TimeProcesser.getUnixTime());
		CrossProxySerialize.getInstance().addFlowerLog(warFlower);
		CrossProxySerialize.getInstance().commitSync();
	}

	public static void flowerCross(final long playerId, final String playerName, final int type) {
		Context.getTimerServer().execute(new Runnable() {

			@Override
			public void run() {
				WarApiClient.crossFlower(playerId, playerName, type);
			}
		});

	}

	public static void areaMessage(final int area, final String method, final Object... parms) {
		areaMessageDelay(area, 0, method, parms);
	}

	public static void areaMessageDelay(final int area, int delay, final String method, final Object... parms) {
		Context.getTimerServer().scheduleSeconds(new Runnable() {

			@Override
			public void run() {
				Collection<CrossServer> servers = CrossServer.getAll();
				for (CrossServer e : servers) {
					if (area <= 0) {
						if (e.getType() <= 0) {
							continue;
						}
					} else if (e.getType() != area) {
						continue;
					}
					WarApiClient.sendApiByTimeOut(e.getServerId(), method, 10000, parms);

				}

			}
		}, delay);

	}

	public static void onceMessage(final int serverId, final String method, final Object... parms) {
		Context.getTimerServer().execute(new Runnable() {

			@Override
			public void run() {
				Object msg = WarApiClient.sendApiByTimeOut(serverId, method, 15000, parms);
				boolean ok = false;
				if (msg == null) {

				} else if (msg instanceof Boolean) {
					ok = (Boolean) msg;
				}
				if (!ok) {
					StringBuilder sb = new StringBuilder();
					sb.append(" serverId ").append(serverId);
					sb.append(" method ").append(method);
					if (parms != null) {
						for (Object e : parms) {
							sb.append("  ").append(e);
						}
					}
					LogUtil.getMain().error(" onceMessage  err " + sb.toString());
				}
			}
		});
	}

	public static void upWarInfo(int area, Object... parms) {
		areaMessage(area, ApiConfig.warApiCam, parms);
	}

	public static void chatMsssage(int area, Object... parms) {
		areaMessage(area, ApiConfig.crossChat, parms);
	}

	public static void chatMsssageDelay(int area, int delay, Object... parms) {
		areaMessageDelay(area, delay, ApiConfig.crossChat, parms);
	}

	public static void warSendAward(int serverId, long uid, int camType) {
		onceMessage(serverId, ApiConfig.warAward, uid, camType);
	}

	public static void sendBetMsg(int serverId, int type, long uid, int camType, boolean suc, int asset, String content) {
		onceMessage(serverId, ApiConfig.warBetAward, type, uid, camType, suc, asset, content);
	}

	public static WarWorldData getWarWorld() {
		return WarWorldSerialize.getInstance().getWarWorldData();
	}

	public static void worldUpWarInfo(Object... parms) {
		areaMessage(0, ApiConfig.worldApiCam, parms);
	}

	public static void worldSendAward(int serverId, long uid, int camType) {
		onceMessage(serverId, ApiConfig.worldAward, uid, camType);
		LogUtil.getMain().debug("world send award s " + serverId + " uid " + uid + " cam " + camType);
	}


}
